<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');


if ( ! function_exists('urlTitle'))
{
	function urlTitle($str, $separator = '-', $lowercase = true)
	{
		$str = preg_replace('/-|\/|\./', ' ', $str);
		// require text-helper
		$str = convert_accented_characters($str);

		return url_title($str, $separator, $lowercase);
	}
}
if ( ! function_exists('siteUrl'))
{
	function siteUrl($uri = '', $category = 0)
	{
		$segs = explode('/', $uri);
		$CI = get_instance();
		if(in_array($uri, $CI->router->routes))
		{
			$key = array_search($uri, $CI->router->routes);
			if($key)
			{
				$uri = $key;
			}
		}
		elseif(strpos($uri, 'detail') !== false)
		{
			$category = intval($category);
			foreach($CI->router->routes as $key=>$val)
			{
				if(strpos($val, $segs[0].'/index/'.$category) !== false)
				{
					$uri = $key.'/'.$segs[2].'.html';
					break;
				}
			}
		}

		return site_url($uri);
	}
}
if ( ! function_exists('redirect'))
{
	function redirect($uri = '', $method = 'location', $http_response_code = 302)
	{
		if ( ! preg_match('#^https?://#i', $uri))
		{
			$uri = site_url($uri);
		}
		$ajax = '';
		if(isset($_GET['ajax']) && $_GET['ajax'])
		{
			$ajax = '?ajax=1';
		}
		$uri .= $ajax;

		switch($method)
		{
			case 'refresh'	: header("Refresh:0;url=".$uri);
				break;
			default			: header("Location: ".$uri, TRUE, $http_response_code);
				break;
		}
		exit;
	}
}